package com.zy.hospitalward_management_system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.hospitalward_management_system.pojo.Drug;
import com.zy.hospitalward_management_system.pojo.Patient;
import com.zy.hospitalward_management_system.service.impl.DrugServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zy
 * @since 2021-01-26
 */
@Controller
public class DrugController {

    @Resource
    DrugServiceImpl drugService;

    /**
     * 传入药品ID 跳转到药品Info页面
     * @param id 药品ID
     * @param model
     * @return
     */
    @GetMapping("/toDrugInfo/{id}")
    public String toDrugInfo(@PathVariable("id") int id, Model model){
        Drug drug = drugService.getBaseMapper().selectById(id);
        model.addAttribute("drug",drug);
        return "user/info/drug_info";
    }

    @GetMapping("/toDrugAdd")
    public String toDrugAdd(){
        return "user/add/drug_add";
    }


    /**
     * 添加药品
     * @param drug 药品对象
     * @return 返回信息
     */
    @PostMapping("/drug/add")
    @ResponseBody
    public String addDrug(Drug drug){

        if(drug.getDrugName().isEmpty())
            return "药品名不能为空!";

        if(drug.getDrugPrice()==null)
            return "药品价格不能为空!";

        if(drug.getDrugInfo().isEmpty())
            return "药品信息不能为空!";

        Drug drugEx = drugService.getBaseMapper().selectOne(new QueryWrapper<Drug>().eq("drug_name", drug.getDrugName()));
        if(drugEx!=null){
            return "该药品已经存在!";
        }

        int insert = drugService.getBaseMapper().insert(drug);
        if(insert==1){
            return "添加成功!";
        }else {
            return "添加失败!";
        }
    }



    /**
     * 传入药品对象 对药品信息进行更新
     * @param drug 药品对象
     * @return 更新结果
     */
    @PostMapping("/drug/update")
    @ResponseBody
    public String updateDrugById(Drug drug){

        if(drug.getDrugName().isEmpty())
            return "药品名不能为空!";

        if(drug.getDrugPrice()==null)
            return "药品价格不能为空!";

        if(drug.getDrugInfo().isEmpty())
            return "药品信息不能为空!";

        int i = drugService.getBaseMapper().updateById(drug);
        if(i==1){
            return "修改成功!";
        }else{
            return "修改失败!";
        }
    }

    /**
     * 根据患者名或者联系方式进行病人管理页面的精确或模糊查询
     */
    @RequestMapping("/drug/search")
    public String patientSearch(String search,Model model){

        if(search==null||search.isEmpty()){
            model.addAttribute("msg","请输入搜索内容!");
            return "forward:/toDrugList";
        }
        search=search.trim();
        List<Drug> drugs = drugService.getBaseMapper().selectList(
                new QueryWrapper<Drug>().like("drug_name", search).or().eq("drug_id", search));
        if(!drugs.isEmpty()){
            model.addAttribute("drugs",drugs);
            return "user/list/drug_list";
        }
        model.addAttribute("msg","未查询到相关记录!");
        return "forward:/toDrugList";
    }


    /**
     * 传入ID,删除药品
     * @param id
     * @return
     */
    @GetMapping("/drug/delete/")
    @ResponseBody
    public String deleteDrugById(String id){
        System.out.println(id);
        int i = drugService.getBaseMapper().deleteById(id);
        if(i==1){
            return "删除成功!";
        }else {
            return "删除失败!";
        }
    }

}

